﻿using System;
using System.Text;
using System.Data;

namespace MyFastDataSourceBind
{
    /// <summary>
    /// 用于存储数据库命令信息的对象
    /// </summary>
    /// <remarks>编制 袁永福</remarks>
    [Serializable()]
    public class XDBCommandInfo
    {
        /// <summary>
        /// 初始化对象
        /// </summary>
        public XDBCommandInfo()
        {
        }

        /// <summary>
        /// 初始化对象
        /// </summary>
        /// <param name="commandText">命令文本</param>
        public XDBCommandInfo(string commandText)
        {
            strCommandText = commandText;
        }

        private string strCommandText = null;
        /// <summary>
        /// 命令文本
        /// </summary>
        public string CommandText
        {
            get { return strCommandText; }
            set { strCommandText = value; }
        }

        private XDBParameterInfoList myParameters = new XDBParameterInfoList();
        /// <summary>
        /// 参数列表
        /// </summary>
        public XDBParameterInfoList Parameters
        {
            get
            { return myParameters; }
        }

        /// <summary>
        /// 根据对象数据创建一个实际使用的数据库命令对象
        /// </summary>
        /// <param name="conn">数据库连接对象</param>
        /// <returns>创建的的数据库命令对象</returns>
        public IDbCommand CreateCommand(IDbConnection conn)
        {
            System.Data.IDbCommand cmd = conn.CreateCommand();
            cmd.CommandText = this.CommandText;
            foreach (XDBParameterInfo p in myParameters)
            {
                IDbDataParameter p2 = cmd.CreateParameter();
                p2.ParameterName = p.Name;
                p2.Value = p.Value;
                p2.SourceColumn = p.SourceColumn;
                cmd.Parameters.Add(p2);
            }
            return cmd;
        }
    }
}